package com.lala.controller;

import com.lala.service.EmailService;
import com.lala.util.ResultJson;
import com.lala.util.VerCodeGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author 霍明铭
 * @date 2020/3/24 22:00 邮件发送
 */
@Controller
public class EmailController {
@Autowired
    EmailService emailService;
@Autowired
    RedisTemplate redisTemplate;
    @GetMapping("/email")
    @ResponseBody
    public ResultJson sendEmail(@RequestParam("emailAddress") String emailAddress) {
        String mailcode=VerCodeGenerateUtil.generateVerCode();
        try {
            emailService.sendEmailVerCode(emailAddress,mailcode );
            redisTemplate.opsForValue().set("mailcode",mailcode);
            redisTemplate.expire("mailcode",1, TimeUnit.MINUTES);
            return new ResultJson(200,"发送成功",null);
        } catch (Exception e) {
            return new ResultJson(500,"发送失败",null);
        }
    }



}
